package com.wude.guild;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AppInfo;
import com.wude.guild.adapter.DownLoadListAdapter;
import com.wude.guild.manager.DownloadManager;
import com.wude.guild.manager.DownloadnumObserver;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.tools.DbUtils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DownloadActivity extends TitleBaseActivity implements DownloadnumObserver {

    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    private DownLoadListAdapter downLoadListAdapter;
    private int num;
    private DbManager db;
    List<AppInfo> all = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        ButterKnife.bind(this);

        setHeaderTitle("下载管理");
        initdata();
    }

    private void initdata() {
        downLoadListAdapter = new DownLoadListAdapter(this);
        list.setAdapter(downLoadListAdapter);
        getDownLoadList();
    }

    public void getDownLoadList() {
        try {
            if (db == null) {
                db = DbUtils.getDB();
            }
            all.clear();
            //Status: 0未下载，1等待中，2下载中，3下载完成未安装，4下载失败，5已经安装，6暂停
            List<AppInfo> dengdai = db.selector(AppInfo.class).where("Status", "=", 1).findAll();
            List<AppInfo> zhong = db.selector(AppInfo.class).where("Status", "=", 2).findAll();
            List<AppInfo> chenggong = db.selector(AppInfo.class).where("Status", "=", 3).findAll();
            List<AppInfo> shibai = db.selector(AppInfo.class).where("Status", "=", 4).findAll();
            List<AppInfo> anzhuang = db.selector(AppInfo.class).where("Status", "=", 5).findAll();
            List<AppInfo> zanting = db.selector(AppInfo.class).where("Status", "=", 6).findAll();
            if ((zhong != null && zhong.size() != 0) || (chenggong != null && chenggong.size() != 0) || (zanting != null && zanting.size() != 0) || (anzhuang != null && anzhuang.size() != 0) || (shibai != null && shibai.size() != 0) || (dengdai != null && dengdai.size() != 0)) {
                errorLayout.setVisibility(View.GONE);
                list.setVisibility(View.VISIBLE);
                if (zhong != null) {
                    Log.e("下载中集合大小：", zhong.size() + "");
                    all.addAll(zhong);
                }
                if (chenggong != null) {
                    Log.e("下载成功集合大小：", chenggong.size() + "");
                    all.addAll(chenggong);
                }
                if (dengdai != null) {
                    Log.e("等待下载集合大小：", dengdai.size() + "");
                    all.addAll(dengdai);
                }
                if (shibai != null) {
                    Log.e("下载失败集合大小：", shibai.size() + "");
                    all.addAll(shibai);
                }
                if (anzhuang != null) {
                    Log.e("已经安装集合大小：", anzhuang.size() + "");
                    all.addAll(anzhuang);
                }
                if (zanting != null) {
                    Log.e("暂停中集合大小：", zanting.size() + "");
                    all.addAll(zanting);
                }
                Log.e("集合大小：", all.size() + "");
                downLoadListAdapter.setList(all);
            } else {
                errorLayout.setVisibility(View.VISIBLE);
                list.setVisibility(View.GONE);
                errorText.setText("当前没有下载任务");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        DownloadManager.getInstance().addnumObserver(this);
        if (downLoadListAdapter != null) {
            downLoadListAdapter.start();
        }

        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        DownloadManager.getInstance().deletenumObserver(this);
        if (downLoadListAdapter != null) {
            downLoadListAdapter.delete();
        }

        MobclickAgent.onPause(this);
    }

    @Override
    public void onDownloadnumChanged(int i) {
        if (num == 0) {
            num = i;
            return;
        }
        if (num != i) {
            getDownLoadList();
        }
    }
}
